define([
  "modernizr",
  "backbone.layoutmanager",
  "plugins/backbone.collectioncache",
  "vendor/bootstrap/js/bootstrap"
],

function() {

  Backbone.Collection.prototype.fetch = function() {
    var fetch = Backbone.Collection.prototype.fetch;
    
    return function() {
      this.trigger("fetch");
      console.log(fetch);
      return fetch.apply(this, arguments);
    };
  }();

  var app = {
    root: "/"
  };

  var JST = window.JST = window.JST || {};

  // Configure LayoutManager with Backbone Boilerplate defaults.
  Backbone.Layout.configure({
    // Allow LayoutManager to augment Backbone.View.prototype.
    manage: true,

    prefix: "app/templates/",

    fetch: function(path) {
      // Concatenate the file extension.
      path = path + ".html";

      // If cached, use the compiled template.
      if (JST[path]) {
        return JST[path];
      }

      // Put fetch into `async-mode`.
      var done = this.async();

      // Seek out the template asynchronously.
      // Seek out the template asynchronously.
      $.get(app.root + path, function(contents) {
        done(JST[path] = _.template(contents));
      });
    }
  });

  // Mix Backbone.Events, modules, and layout management into the app object.
  return _.extend(app, {
    // Create a custom object with a nested Views object.
    module: function(additionalProps) {
      return _.extend({ Views: {} }, additionalProps);
    },

    // Helper for using layouts.
    useLayout: function(name, options) {
      // Enable variable arity by allowing the first argument to be the options
      // object and omitting the name argument.
      if (_.isObject(name)) {
        options = name;
      }

      // Ensure options is an object.
      options = options || {};

      // If a name property was specified use that as the template.
      if (_.isString(name)) {
        options.template = name;
      }

      var layout = new Backbone.Layout(_.extend({
        el: "#layout"
      }, options));

      // Check if a layout already exists, if so, update the template.
      // if (this.layout) {
      //   this.layout.template = options.template;
      // } else {
      //   // Create a new Layout with options.
      //   this.layout = new Backbone.Layout(_.extend({
      //     el: "#layout"
      //   }, options));
      // }

      // Cache the reference.
      return this.layout = layout;
    }
  }, Backbone.Events);

});
